﻿<template>
  <a-modal
    :title="title"
    width="50%"
    :visible="visible"
    :confirmLoading="loading"
    okText="选择"
    @ok="handleChoose"
    @cancel="visible=false"
  >
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="16">
          <a-col :md="5" :sm="24">
            <a-form-item label="查询类别">
              <a-select v-model="queryParam.condition">
                <a-select-option key="code">物料编号</a-select-option>
                <a-select-option key="name">物料名称</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input allowClear @keydown="inputKeyDown" v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: tableHight }"
      :columns="columns"
      :rowKey="(row) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      @change="handleTableChange"
      :rowSelection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
          filterMultiple: false,
          type: 'radio',
        }"
      :bordered="true"
      :loading="loading"
      size="small"
    >
    </a-table>
  </a-modal>
</template>

<script>
const columns = [
  { title: '物料编号', dataIndex: 'code', align: 'center', width: 200 },
  { title: '物料名称', dataIndex: 'name', align: 'center', width: 200 },
  { title: '助记码', dataIndex: 'shorthand', align: 'center', width: 200 },
  { title: '规格型号', dataIndex: 'specification', align: 'center', width: 200 }
]

export default {
  mounted () {
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHight = this.getTableHeight(true) + 24.5
    })
    this.getDataList()
  },
  data () {
    return {
      tableHight: 570, // 表格高度
      data: [],
      visible: false,
      title: '',
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRows: [],
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    openForm (title) {
      this.data = []
      this.title = title
      this.queryParam = {}
      this.visible = true
      this.selectedRows = []
      console.log(this.selectedRows)
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/MaterialManagement/aa_inventory/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    inputKeyDown (e) {
      if (e.keyCode === 13) {
        if (!this.queryParam[0].keyword) return this.$message.error('请输入查询条件')
        this.getDataList()
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleReturnMain () {
      this.getDataList()
    },
    handleChoose () {
      console.log(this.selectedRows)
      if (this.selectedRows.length > 0) {
        this.visible = false
        this.$emit('inputInv', this.selectedRows)
      } else {
        this.$message.error('请选择后确认')
      }
    },
    // 表格点击选中取消
    loadRow (record, index) {
      return {
        on: {
          click: () => {
            // 记录为disabled则直接返回，默认为不可选
            if (record.disabled) return
            const type = 'checkbox'
            const key = record.Id
            let selectedRows = this.selectedRows
            let selectedRowKeys = this.selectedRowKeys
            if (type === 'radio') {
              selectedRowKeys = [key]
              selectedRows = [record]
            }
            if (!this.selectedRowKeys.includes(key)) {
              selectedRowKeys.push(key)
              selectedRows.push(record)
            } else {
              const index = this.selectedRowKeys.findIndex(skey => skey === key)
              selectedRows.splice(index, 1)
              selectedRowKeys.splice(index, 1)
            }
            this.updateSelect(selectedRowKeys, selectedRows)
            this.onSelectChange(selectedRowKeys, selectedRows)
          }
        }
      }
    },
    // 更新选中操作
    updateSelect (selectedRowKeys, selectedRows) {
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    }
  }
}
</script>
